package com.muqi.app.qlearn.student;

import com.muqi.app.im.widget.LoadToast;
import com.muqi.app.loadingview.SVProgressHUD;
import com.muqi.app.network.http.AsyncCacheHttpForGet;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.project.contants.MContants;
import com.muqi.app.sharepreference.SharePreferenceUtil;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public abstract class BaseFragmentActivity extends BaseWindowActivity {
	protected Context mContext;
	private ProgressBar progressbar;
	/** user登录配置 */
	public SharePreferenceUtil mSpUtil;
	protected LoadToast mLoadtoast;
	protected SVProgressHUD mSVProgressHUD;
	private FrameLayout mFrameLayout;
	protected View emptyView;
	protected TextView emptyText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.mContext = this;
		mSpUtil = new SharePreferenceUtil(mContext, MContants.UserLogin);
	}

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		init();
	}

	@Override
	public void setContentView(View view, ViewGroup.LayoutParams params) {
		super.setContentView(view, params);
		init();
	}

	private void init() {
		onInit();
		onData();
	}

	/**
	 * 初始化控件
	 */
	protected abstract void onInit();

	/**
	 * 初始化数据
	 */
	protected abstract void onData();
	
	protected void showLoadToast(String loadMsg){
		if(mLoadtoast==null){
			mLoadtoast = new LoadToast(this);
		}
		mLoadtoast.setProgressColor(Color.RED)
				.setText(loadMsg)
				.setTranslationY(200).show();
	}
	/**
	 * 进度条+文字提醒
	 * @param message
	 */
	public void showProgressWithState(String message){
		if(mSVProgressHUD == null){
			mSVProgressHUD = new SVProgressHUD(this);
		}
		mSVProgressHUD.showWithStatus(message, SVProgressHUD.SVProgressHUDMaskType.Gradient);
	}
	/**
	 * success
	 */
	public void showSuccessWithState(){
		if(mSVProgressHUD == null){
			mSVProgressHUD = new SVProgressHUD(this);
		}
		mSVProgressHUD.showSuccessWithStatus("恭喜,提交成功!");
	}
	/**
	 * 进度条+数字进度
	 * @param progress
	 */
	public void showSvWithProgress(int progress){
		if(mSVProgressHUD == null){
			mSVProgressHUD = new SVProgressHUD(this);
			mSVProgressHUD.getProgressBar().setProgress(0);
		    mSVProgressHUD.showWithProgress("进度 " + 0 + "%", SVProgressHUD.SVProgressHUDMaskType.Gradient);
		}
	    if (mSVProgressHUD.getProgressBar().getMax() != mSVProgressHUD.getProgressBar().getProgress()){
			mSVProgressHUD.getProgressBar().setProgress(progress);
		    mSVProgressHUD.setText("进度 "+progress+"%");
		}
	}
	/**
	 * err 
	 */
	public void showErrorWithState(){
		if(mSVProgressHUD == null){
			mSVProgressHUD = new SVProgressHUD(this);
		}
		mSVProgressHUD.showErrorWithStatus("抱歉,请求失败!");
	}
	
	public void setMaxPicsSvWithProgress(int maxNo, String prgMsg){
		if(mSVProgressHUD == null){
			mSVProgressHUD = new SVProgressHUD(this);
			mSVProgressHUD.getProgressBar().setProgress(0);
		    mSVProgressHUD.showWithProgress(prgMsg + 0 + "/" + maxNo, SVProgressHUD.SVProgressHUDMaskType.Gradient);
		}
	}
	
	public void hidingSVloading(){
		if(mSVProgressHUD!=null && mSVProgressHUD.isShowing()){
			mSVProgressHUD.dismissImmediately();
		}
	}

	/**
	 * 展示progressbar
	 */
	public void showLoading() {
		showLoading(0, 0);
	}

	/**
	 * 展示progressbar
	 * 
	 * @param topSpace
	 *            progressbar与顶部的距离
	 * @param bottomSpace
	 *            progressbar与底部的距离
	 */
	public void showLoading(int topSpace, int bottomSpace) {
		if (progressbar != null) {
			if (!progressbar.isShown()) {
				FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) progressbar
						.getLayoutParams();
				if (layoutParams.topMargin != topSpace
						|| layoutParams.bottomMargin != bottomSpace) {
					layoutParams.topMargin = topSpace;
					layoutParams.bottomMargin = bottomSpace;
					progressbar.setLayoutParams(layoutParams);
				}
				mFrameLayout.setVisibility(View.VISIBLE);
				progressbar.setVisibility(View.VISIBLE);
			}
		} else {
			mFrameLayout = new FrameLayout(mContext);
			mFrameLayout.setLayoutParams(new FrameLayout.LayoutParams(
					FrameLayout.LayoutParams.MATCH_PARENT,
					FrameLayout.LayoutParams.MATCH_PARENT));
			mFrameLayout.setOnClickListener(null);
			
			
			progressbar = new ProgressBar(mContext);
			FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
					150, 150);
			progressbar.setLayoutParams(layoutParams);
			layoutParams.topMargin = topSpace;
			layoutParams.bottomMargin = bottomSpace;
			layoutParams.gravity = Gravity.CENTER;
			mFrameLayout.addView(progressbar);
			
			((FrameLayout) getWindow().findViewById(
					Window.ID_ANDROID_CONTENT)).addView(mFrameLayout);
		}
	}

	/**
	 * 隐藏progressbar
	 */
	public void hideLoading() {
		if (progressbar != null) {
			mFrameLayout.setVisibility(View.GONE);
			progressbar.setVisibility(View.GONE);
		}
	}


	public void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
	
	/**
	 * Activity跳转
	 * 
	 * @param cls
	 */
	public void startActivity(Class<?> cls) {
		Intent intent = new Intent(this, cls);
		startActivity(intent);
	}

	@Override
	public void startActivity(Intent intent) {
		super.startActivity(intent);
	}

	/**
	 * 关闭系统的软键盘
	 * 
	 * @param activity
	 */
	public void dismissSoftKeyboard() {
		View view = this.getWindow().peekDecorView();
		if (view != null) {
			InputMethodManager inputmanger = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
			inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}
	/**
	 * view为接受软键盘输入的视图
	 * @param view
	 */
	public void getSoftInput(View view) {
	    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
	    imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);  
	}
	
	@Override
	protected void onDestroy() {
        MyAsyncHttp.cancelRequest(this);
        AsyncCacheHttpForGet.cancelRequest(this);
		super.onDestroy();
	}
	protected void showEmpty(String msg) {
		if (emptyView == null) {
			emptyText = (TextView) findViewById(R.id.empty_tv);
			emptyText.setText(msg);
			emptyView = findViewById(R.id.empty);
			emptyView.setVisibility(View.VISIBLE);
		} else {
			emptyView.setVisibility(View.VISIBLE);
		}

	}

	protected void hidEmpty() {
		if (emptyView == null) {
			emptyText = (TextView) findViewById(R.id.empty_tv);
			emptyView = findViewById(R.id.empty);
			emptyView.setVisibility(View.GONE);
		}else{
			emptyView.setVisibility(View.GONE);
		}
	}
}

